var express = require("express");
var app = express();
app.use(express.static("static"));
var mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1/food"); //连接数据库
var star = require("../models/star.js").star;
var food = [];
/*{
    name: "明辉拉肠",
    star: 3
}, {
    name: "木桶饭",
    star: 2
}, {
    name: "一记茶餐厅",
    star: 5
}, {
    name: "隆江猪脚饭",
    star: 5
}*/


//建立路由
app.get("/", function(req, res) {
    res.send("欢迎来到小众点评");
});
//建立一个接口,把相关的数据返回
app.get("/getFoodData", function(req, res) {
    //取数据库查找相关的信息
    star.find({}, null, function(err, docs) {
        food = docs;
        res.send(food);
    })
})
app.get("/del", function(req, res) {
    var index = req.query.index;
    food.splice(index, 1);
    res.send(food);
})
app.get("/add", function(req, res) {
    var newFood = req.query.newFood;
    food.push(newFood);
    res.send(food);
})
app.get("/changeStar", function(req, res) {
    var index = req.query.index;
    var starIndex = req.query.starIndex;
    food[index].star = starIndex;
    res.send(food);
})
app.listen(8888);
console.log("开始运行服务器");